// manager/pages/knowledgelibrary/index.js
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeIndex: 0,
		tabList: [],
        showFilter: false,
        params: {
            libId: "",
            libTitle: "",
        },
        list: []
        // scroller: {
		// 	pageNum: 0,
		// 	pageSize: 10,
		// 	totalPage: 10,
		// 	isNoneLoading: true,
		// 	refreshColor: '#000000',
		// 	refreshDiy: false,
		// 	isloaded: false,
		// 	list: []
        // },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.findServeLibraryList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.findServeLibraryList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    
    /**
     * 查询知识文库类型接口
     */
    findServeLibraryList() {
        request({
            url: `/ecard/serveLibrary/findServeLibraryList`,
            method: "GET",
            reqRSAENCRY: false, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                this.setData({
                    tabList: res.data,
                })
                this.resetData()
                this.getData();
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 重置数据
     */
    resetData(){
		this.resetParams();
		// this.resetScroller();
    },
    /**
     * 重置下拉
     */
    resetScroller(){
        let scroller = {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
		}
		this.setData({
            scroller,
		})
    },
    /**
     * 验证params是否有值
     */
    validateParams(){
        let isFilter = false;
        let { params } = this.data;
        for(let key in params){
            console.log(params[key])
            if(key!='libId'&&params[key]){
                isFilter = true;
            }
        }
        this.setData({
            isFilter
		})
    },
    /**
     * 重置参数
     */
    resetParams(){
        let { params } = this.data;
        Object.keys(params).forEach((key) => (params[key] = ''))
		this.setData({
            params,
            isFilter: false
		})
    },
    /** 分类点击事件*/
    tabChange(e) {
        let { index } = e.currentTarget.dataset;
        this.setData({
            activeIndex: index
        })
        this.resetData()
        this.getData();
    },
    /**
     * 显示筛选
     */
    showFilterPopup(){
        this.setData({
            showFilter: true
        })
    },
    /**
     * 关闭筛选
     */
    closeFilter(){
        this.setData({
            showFilter: false
        })
    },
    /**
     * 确认筛选
     */
    confirmFilter(){
        this.setData({
            showFilter: false
        })
        this.validateParams();
        // this.resetScroller();
        this.getData();
    },
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.target.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
            // isFilter: !!e.detail.value,
        })
    },
    // 搜索
    searchConfirm(e){
        console.log(e.detail.value)
        this.setData({
            'params.libTitle': e.detail.value
        })
        this.validateParams();
        // 页面加载时执行一次加载
		// this.resetScroller()
        this.getData();
    },

    /**
     * 获取数据
     */
    getData() {
        this.findServeLibraryNewsList();
    },
    /**
     * 获取文库模块文库信息列表
     */
    findServeLibraryNewsList() {
		// let { scroller } = this.data;
        // scroller.pageNum = scroller.pageNum + 1;
        // if(scroller.pageNum>scroller.totalPage){
        //     return
        // }
        let { activeIndex, tabList, params } = this.data;
        params.libId = tabList[activeIndex].id;
        request({
            url: `/ecard/serveLibrary/findServeLibraryNewsList`,
            reqRSAENCRY: false, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
            data: {
                ...params
            }
        }).then(res => {
            if(res.code == 0){
                // scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                // if(scroller.pageNum>=scroller.totalPage){
                //     scroller.isNoneLoading = false;
                // }
                // if(res.data.length>0){
                //     res.data.map(item => {
                //         item.creationTime = util.formatTime(new Date(item.creationTime));
                //         item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                //         return item
                //     })
                // }
                // if(scroller.pageNum == 1){
                //     scroller.list = res.data;
                // }else{
                //     scroller.list = scroller.list.concat(res.data);
                // }
                this.setData({
                    list: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 详情
     */
    toKnowledgelibrarydesc(e){
        let { id } = e.currentTarget.dataset;
        let { activeIndex, tabList } = this.data;
        wx.navigateTo({
          url: `/manager/pages/knowledgelibrarydesc/index?typeName=${tabList[activeIndex].libraryName}&id=${id}`,
        })
    },
})